/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mz2d.render;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import mz2d.graphics.sprite.impl.DrawingEntity;

/**
 *
 * @author Laptop
 */
public class Projector {

    private final static int maxLayers = 100;
    /**
     *
     */
    private MZ2DApplet applet;
    /**
     *
     */
    private Image gameScreen;
    /**
     * 
     */
    private HashMap<Integer, DrawingEntity> entities;

    public Projector(MZ2DApplet applet) {
        this.applet = applet;
        entities = new HashMap<Integer, DrawingEntity>();
    }

    /**
     * 
     * @param entity
     * @return
     */
    public int addEntity(DrawingEntity entity) {
        int idx = entities.size();
        if (entities.containsKey(idx)) {
            idx++;
            while (entities.containsKey(idx)) {
                idx++;
            }
        }
        entities.put(idx, entity);
        return idx;
    }

    /**
     *
     * @param eId
     * @return
     */
    public DrawingEntity getEntity(int eId) {
        return entities.get(eId);
    }

    /**
     * 
     * @param eId
     * @return
     */
    public DrawingEntity deleteEntry(int eId) {
        return entities.remove(eId);
    }

    /**
     *  Renders the frame, so it's initially "black" each frame
     *  thus preventing any weird overlapping/flickering
     * @throws ProjectorException
     * ProjectorException is thrown when java.awt is unable to create a image
     * component out of the "gameScreen"
     */
    public void renderFrame() throws ProjectorException {
        if (gameScreen == null) {
            gameScreen = applet.getDrawingComponent().createImage(applet.width, applet.height);
            if (gameScreen == null) {
                throw new ProjectorException("Could not create game screen.");
            }
        }
        Graphics g = gameScreen.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, applet.width, applet.height);
    }

    /**
     * Renders all entities based on their layer and position in queue
     * @throws ProjectorException
     */
    public void renderEntities() throws ProjectorException {
        Graphics g = gameScreen.getGraphics();

        for (int i = 0; i < maxLayers; i++) {
            Iterator<DrawingEntity> itEntities = entities.values().iterator();
            DrawingEntity drawingEntity;
            while (itEntities.hasNext()) {
                drawingEntity = itEntities.next();
                if (drawingEntity.getLayer() == i) {
                    g.drawImage(drawingEntity.getImage(), drawingEntity.getDrawPosX(), drawingEntity.getDrawPosY(), null);
                }
            }
        }
    }

    public void project() throws ProjectorException {
        try {
            applet.getDrawingComponent().getGraphics().drawImage(gameScreen, 0, 0, null);
        } catch (Exception ex) {
            throw new ProjectorException(ex.getMessage());
        }
    }

    public Graphics getGraphics() {
        return gameScreen.getGraphics();
    }
}
